import {shareUrl} from '../services/login'
import Vue from 'vue'
import {fetchGet, fetchPost} from '../utils/axiosConfig'
import store from '../store'
import tools from '../utils/tools'

var wxConfig = {
    // 获取url参数
    getParams: function (params) {
        let url = decodeURI(location.href);
        let paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");

        for (let i = 0; i < paraString.length; i++) {
            let _key = paraString[i].split('=')[0];
            let _val = paraString[i].split('=')[1];
            if (_key === params) return _val;
        }
        return '';
    },

    // 设置配置信息
    // 调用微信jdk 配置（修改this）
    wxConfigInfo(callback) {
        fetchPost(shareUrl, {url: window.location.href}).then((data) => {

            this.loading = false;
            if (data.code == 200) {
                let _data = data.data;

                // 微信配置，调用微信jdk之前 做的配置
                wx.config({
                    debug: true,
                    appId: _data.appId,
                    timestamp: _data.timestamp,
                    nonceStr: _data.nonceStr,
                    signature: _data.signature,
                    jsApiList: ['checkJsApi', 'getLocation', 'getNetworkType', 'previewImage',
                        'translateVoice', 'chooseImage', 'uploadImage', 'onMenuShareTimeline',
                        'onMenuShareAppMessage', 'onMenuShareQQ', 'updateAppMessageShareData', 'chooseWXPay']
                });
                wx.ready(function () {
                    console.log('wx ready');
                    let user_token = tools.getParams("user_token");
                    store.dispatch('changeToken', user_token)
                    wx.getLocation({
                        type: 'wgs84', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
                        success: function (res) {
                            tools.setStorageParam('localStorage', 'longitude', res.longitude)
                            tools.setStorageParam('localStorage', 'dimensionality', res.latitude)
                            console.log(res, 111)
                            alert(JSON.stringify(res))
                        },
                        error: function (res) {
                            tools.setStorageParam('localStorage', 'longitude', 106.57)
                            tools.setStorageParam('localStorage', 'dimensionality', 29.55)
                        }
                    });

                    tools.goPage('index')

                    wx.checkJsApi({
                        jsApiList: ['updateTimelineShareData',
                            'updateAppMessageShareData',
                            'onMenuShareAppMessage',
                            'onMenuShareTimeline'
                        ], // 需要检测的JS接口列表，所有JS接口列表见附录2,
                        success: function (res) {
                            // 以键值对的形式返回，可用的api值true，不可用为false
                            // 如：{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
                            alert(JSON.stringify(res));
                        },
                        error: function (res) {
                            alert(res);
                        }
                    })

                    callback && callback(_data)
                });
            } else {
                this.show2 = true;
                this.warntext = "微信配置失败";
            }
        }, (err) => {
            this.loading = false;
            this.show2 = true;
            this.warntext = "微信配置失败";
        })
    },

    // 分享给好友
    ShareAppMessage(title, desc, link, imgurl, successCallback, cancelCallback, type = 'link') {
        wx.updateAppMessageShareData({//分享给好友
            title: title, // 分享标题
            desc: desc, // 分享描述
            link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: imgurl, // 分享图标
            type: type, // 分享类型,music、video或link，不填默认为link
            dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
            success: function () {
                // 用户点击了分享后执行的回调函数
                successCallback
            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
                cancelCallback
            }
        })
    },
}

export default wxConfig
